package 
{
    import choppingblock.video.*;
    
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.display.StageDisplayState;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.net.URLRequest;
    
    import org.hive.managers.plugin.LayoutManager;
    import org.hive.managers.plugin.LoadManager;
    import org.hive.sequence.ActionSequence;
    import org.hive.sequence.CloseSequence;
    import org.hive.state.IState;
    import org.hive.state.State;
    
  	
    public class FullScreenYouTubeDemoState extends State implements IState
    {
    	
    	private var ldm:LoadManager;  
    	private var lm:LayoutManager; 
    	
    	private var _youTubeLoader:YouTubeLoader;
    	private var _youTudeData:YouTubeDataAPI;
    	
    	private var _scaleUpButton:SpriteButton; 
        private var _scaleDownButton:SpriteButton; 
        private var _muteButton:SpriteButton; 
        private var _volumeButton:SpriteButton; 
        private var _playButton:SpriteButton; 
        private var _pauseButton:SpriteButton; 
        private var _seekButton:SpriteButton; 
        private var _cc:Sprite; 
        private var _fullscreen:SpriteButton; 
        private var _loadMostPopular:SpriteButton;
        
        private var _grid:Loader;
        private var _bar:Loader;
            
        private var _youtubeUnionChat:YouTubeUnionChat;
    	
        public function create():ActionSequence
        {
            
     	 	ldm = LoadManager.init(hive);
      	 	lm = LayoutManager.init(hive, hive.canvas);
               
            var seq:ActionSequence = new ActionSequence('Main');
                            
			addYoutube();
           	createAPI();
            
            return seq;
        }
        
        
        private function createFullscreenTest():void
        {
        	var fullscreenSprite:Sprite = new Sprite();
        	
        	fullscreenSprite.graphics.beginFill(0xFF0000, 1);
        	fullscreenSprite.graphics.drawRect(0,0,lm.stage.stageWidth, lm.stage.stageHeight);
        	fullscreenSprite.graphics.endFill();
        	
        	lm.addChild(fullscreenSprite);
        }
	
		private function addYoutube():void
		{
           _youTubeLoader = new YouTubeLoader() 
            
            _youTubeLoader.addEventListener(YouTubeLoaderEvent.LOADED, youTubePlayerLoadedHandler, false, 0, true);
			_youTubeLoader.addEventListener(YouTubeLoaderEvent.STATE_CHANGE, youTubePlayerStateChangeHandler, false, 0, true);
			_youTubeLoader.create();
			_youTubeLoader.x = 0;
			_youTubeLoader.y = 0;
			
			_youTubeLoader.visible = false;
		                    
            // ADD VIDEO            
            lm.addChild( _youTubeLoader );
        
   		}
   		     
        private function createAPI():void
        {
        	_youTudeData = new YouTubeDataAPI();
        }            
        
        private function addUnion():void
        {
        	_youtubeUnionChat = new YouTubeUnionChat();
            
            _youtubeUnionChat.alpha = 0.25;
       		lm.addChild(_youtubeUnionChat);       	
        }
        
        private function createButtons():void
        {
        	
        	_scaleUpButton = new SpriteButton("scaleUp", "scale up");
           	_scaleDownButton = new SpriteButton("scaleDown", "scale down");
           	_muteButton = new SpriteButton("mute", "mute");
           	_volumeButton = new SpriteButton("volume", "volume");
           	_playButton = new SpriteButton("play", "play");
           	_pauseButton = new SpriteButton("pause", "pause");
           	_seekButton = new SpriteButton("seek", "seek");
           	_loadMostPopular = new SpriteButton("loadMostPopular", "load most popular");
           	
           	_cc = new Sprite();
           	
           	_fullscreen = new SpriteButton("fullscreen", "fullscreen");
                          	           	
           	var _ccImage:Loader = new Loader();
           	
           	var urlImage:URLRequest = new URLRequest("http://creativecommons.org/images/public/cc-LGPL-a.png")
           	
           	_ccImage.load(urlImage);
           	           	
           	Sprite(_cc).addChild( _ccImage);
           	
           	_scaleDownButton.addEventListener(MouseEvent.CLICK, youtubeSetSizeDownHandler, false, 0, true);
        	_scaleUpButton.addEventListener(MouseEvent.CLICK, youtubeSetSizeUpHandler, false, 0, true);
        	_volumeButton.addEventListener(MouseEvent.CLICK, youtubeVolumeHandler, false, 0, true); 	
        	_muteButton.addEventListener(MouseEvent.CLICK, youtubeUnMuteHandler, false, 0, true);
        	_playButton.addEventListener(MouseEvent.CLICK, youtubePlayHandler, false, 0, true);
        	_pauseButton.addEventListener(MouseEvent.CLICK, youtubePauseHandler, false, 0, true);
        	_cc.addEventListener(MouseEvent.CLICK, ccHandler, false, 0, true);
        	_fullscreen.addEventListener(MouseEvent.CLICK, fullscreenHandler, false, 0, true);
        	_loadMostPopular.addEventListener(MouseEvent.CLICK, loadMostPopular, false, 0, true);
        	
        	_scaleUpButton.x = 500;
        	_scaleUpButton.y = 50;
        		
        	_scaleDownButton.x = 500;
        	_scaleDownButton.y = 100;
        	
        	_muteButton.x = 500;
        	_muteButton.y = 150;
        	
        	_volumeButton.x = 500;
        	_volumeButton.y = 150;
        	
        	_playButton.x = 500; 
        	_playButton.y = 200;
        	
        	_pauseButton.x = 500; 
        	_pauseButton.y = 250;
        	
        	_fullscreen.x = 500; 
        	_fullscreen.y = 300;
        	
        	_loadMostPopular.x = 500;
        	_loadMostPopular.y = 350;
        	
        	_cc.x = 50;
        	_cc.y = 550;
        	        	
        	lm.addChild(_scaleUpButton);
        	lm.addChild(_scaleDownButton);
         	lm.addChild(_muteButton);
         	lm.addChild(_volumeButton);
            lm.addChild(_playButton);
            lm.addChild(_pauseButton);
            lm.addChild(_cc);
            lm.addChild(_fullscreen);
            lm.addChild(_loadMostPopular);
            
        }
        
        private function addGrid():void
        {
        	_grid = new Loader();
        	
        	Loader(_grid).load( new URLRequest("images/grid.png") );
        	//Loader(_grid).addEventListener(Event.COMPLETE, gridLoadCompleteHandler);
        	
        	 _grid.width = 2000;//lm.stage.stageWidth;
			 _grid.height = 2000;//lm.stage.stageHeight;
        	             	
        	 lm.addChild(_grid);
      
  
        }
        
        private function addBar():void
        {
        	/*
        	var alphaSequence:ActionSequence = new ActionSequence("lowerNavSequence");
			
			// BAR 
			
			var animalObj:Object = {
								scope: _bar,
								alpha: 1,
								time: 0.5,
								transition:"easeoutexpo"
								}
								
			alphaSequence.push( _bar );
			
			/alphaSequence.
			*/
			
			_bar = new Loader();
        	
        	Loader(_bar).load( new URLRequest("images/bar.png") );
        	        	
        	_bar.alpha = 1;
        	
        	_bar.mouseEnabled = true;
        	_bar.mouseChildren = false;
        	
        	_bar.x = (lm.stage.stageWidth / 2) - 250;
        	_bar.y = (lm.stage.stageHeight) - 170;
        	
        	 lm.addChild(_bar);
        	
        }
        
        
        
        private function gridLoadCompleteHandler(e:Event):void
        {
        	// fade in grid
        	
        	 _grid.alpha = 0.10;
        	
        	_grid.mouseEnabled = true;
        	_grid.mouseChildren = false;
        	
        	 _grid.width = lm.stage.stageWidth + 200;
			 _grid.height = lm.stage.stageHeight + 200;
        	             	
        	 lm.addChild(_grid);
        }

		private function handleMouseClick(e:MouseEvent):void
		{
			
		}
		
		/**
		Called via player loaded event, lets you know you are all clear to send player commands.
		*/
		private function youTubePlayerLoadedHandler (event:YouTubeLoaderEvent):void
		{
			_youTubeLoader.visible = true;
			
			var terraI:String = "plyRYrqa_4g";
			var blastID = "OMv8728uUFY";
			var massEffectID = "qsefCmNAQR4";
			
			_youTubeLoader.loadVideoById(blastID);
			
			_youTubeLoader.setSize(lm.stage.stageWidth, lm.stage.stageHeight);
			
			_youTubeLoader.mouseEnabled = false;
									 
			//lm.stage.addEventListener(Event.RESIZE, onResizeHandler, false, 0, true);   
            //lm.stage.dispatchEvent( new Event(Event.RESIZE) );
			 
			addGrid();
			createButtons();
			   
			 addBar();
		};
		
		private function onResizeHandler(e:Event):void
		{
			/*
			 if ( _grid != null )
			 {
			 _grid.width = lm.stage.stageWidth + 100;
			 _grid.height = lm.stage.stageHeight + 100;
			 }
			 */
		}
		
		private function loadMostPopular(e:MouseEvent):void
		{
			var mostPopularVideoID:String = _youTudeData.getMostPopularVideoID();
			
			_youTubeLoader.clearVideo();
			
			trace("most popular: " + mostPopularVideoID);
			_youTubeLoader.loadVideoById(mostPopularVideoID);
		}
				
		/**
		Called every time the player state changes
		*/
		private function youTubePlayerStateChangeHandler (event:YouTubeLoaderEvent):void
		{
			//trace("YouTubeIntegrationDemo: youTubePlayerStateChangeHandler");
			//_stateField.text = event.state;
		};
		
		private function youtubeLoadVideoHandler (event:Event):void{
			_youTubeLoader.loadVideoById(event.target.selectedItem.data)
		};
		
		private function youtubeSetSizeUpHandler (event:MouseEvent):void
		{
			_youTubeLoader.setSize(620, 465)
		};
		
		private function youtubeClearHandler (event:MouseEvent):void
		{
			//trace("YouTubeIntegrationDemo: youtubeClearHandler");
			// i have found it's best to stop then clear the video
			_youTubeLoader.stop()
			_youTubeLoader.clearVideo()
		};
		
		private function youtubeSetSizeDownHandler (event:MouseEvent):void
		{
			//trace("YouTubeIntegrationDemo: youtubeSetSizeDownHandler");
			_youTubeLoader.setSize(320, 240)
		};
		
		private function youtubePlayHandler (event:MouseEvent):void{
			//trace("YouTubeIntegrationDemo: youtubePlayHandler");
			_youTubeLoader.play()
		};
		
		private function youtubePauseHandler (event:MouseEvent):void
		{
			//trace("YouTubeIntegrationDemo: youtubePauseHandler");
			_youTubeLoader.pause()
		};
		
		private function youtubeVolumeHandler (event:MouseEvent):void
		{
			//trace("YouTubeIntegrationDemo: youtubeVolumeHandler");
			_youTubeLoader.setVolume(Number(event.target.args));
		};
		
		private function youtubeMuteHandler (event:MouseEvent):void
		{
			//trace("YouTubeIntegrationDemo: youtubeMuteHandler");
			_youTubeLoader.mute();
		};
		
		private function youtubeUnMuteHandler (event:MouseEvent):void
		{
			//trace("YouTubeIntegrationDemo: youtubeUnMuteHandler");
			_youTubeLoader.unMute();
		};
		
		private function ccHandler (event:MouseEvent):void
		{
						 
		};
		
		private function fullscreenHandler (event:MouseEvent):void
		{
			 lm.stage.displayState = StageDisplayState.FULL_SCREEN;
			 
			 _youTubeLoader.x = 0;
			 _youTubeLoader.y = 0;
			 _youTubeLoader.setSize(lm.stage.fullScreenWidth, lm.stage.fullScreenHeight);
			 
			 _grid.width = lm.stage.fullScreenWidth;
			 _grid.height = lm.stage.fullScreenHeight;
			 
			_bar.x = (lm.stage.fullScreenWidth / 2) - (_bar.width / 2);
        	_bar.y = (lm.stage.fullScreenHeight / 2) - (_bar.height / 2);
			 
			 
		};
		
		private function maintainAspectRatio():void
		{
			/*
			// we will go to full screen zoomed in on the you tube player
		    var fullScreenRect:Rectangle = new Rectangle(_youTubeLoader.x, _youTubeLoader.y, _youTubeLoader.width, _youTubeLoader.height);
		
		    // calculate aspect ratio of the red square
		    var rectAspectRatio:Number = fullScreenRect.width / fullScreenRect.height;
		
		    // calculate aspect ratio of the screen
		    var screenAspectRatio:Number = stage.fullScreenWidth / stage.fullScreenHeight;
		
		    // change the fullScreenRect so that it covers the entire screen, keeping it centered on the redSquare
		    // try commenting out this section to see what happens if you do not fix the aspect ratio.
		    
		    if (rectAspectRatio > screenAspectRatio) {
		         var newHeight:Number = fullScreenRect.width / screenAspectRatio;
		         fullScreenRect.y -= ((newHeight - fullScreenRect.height) / 2);
		        fullScreenRect.height = newHeight;
		    } else if (rectAspectRatio < screenAspectRatio) {
		        var newWidth:Number = fullScreenRect.height * screenAspectRatio;
		        fullScreenRect.x -= ((newWidth - fullScreenRect.width) / 2);
		        fullScreenRect.width = newWidth;
		    }
		    */
		  
		}
		
		public function close():CloseSequence
        {
            return null;
        } 
              
	}
	
	
	
	

}



